<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <a-drawer
    :title="!dataForm.id ? '添加' : '修改'"
    :visible="visible"
    @close="cancel"
    :body-style="{ paddingBottom: '80px' }"
    :width="750"
  >
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule" v-loading="loading" ref="dataForm" @keyup.enter.native="dataSubmit()"
             label-width="120px" style="padding: 0px 10px;margin: 0px 10px">

      <el-form-item label="请选择app" prop="appid" >
        <el-select v-model="dataForm.appid" placeholder="请选择app" class="width100" filterable clearable>
          <el-option
            v-for="item in applist"
            :key="item.appid"
            :value="item.appid"
            :label="item.appname">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="icon" prop="icon">
        <upload-img text="上传" :fileListValue="dataForm.icon" v-model="dataForm.icon"></upload-img>
      </el-form-item>
      <el-form-item label="任务名称" prop="taskname">
        <el-input v-model="dataForm.taskname" placeholder="请输入任务名称" size="small"></el-input>
      </el-form-item>
      <el-form-item label="任务描述">
        <el-input v-model="dataForm.taskinfo" placeholder="请输入任务描述" size="small"></el-input>
      </el-form-item>
      <el-form-item label="金币" prop="coin">
        <el-input type="number" v-model="dataForm.coin" placeholder="请输入金币" size="small"></el-input>
      </el-form-item>
      <el-form-item label="任务数量" prop="limitcount">
        <el-input type="number" v-model="dataForm.limitcount" placeholder="请输入任务数量" size="small"></el-input>
      </el-form-item>
      <el-form-item label="广告类型 " prop="adtype">
<!--        <el-input v-model="dataForm.adtype" placeholder="请输入广告类型 " size="small"></el-input>-->
        <el-select v-model="dataForm.adtype" placeholder="请输入广告类型" class="width100" filterable clearable>
          <el-option
            v-for="item in selOptions.adType"
            :key="item.value"
            :value="item.value"
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="广告平台 " prop="limitcount">
        <el-select v-model="dataForm.adplatform" placeholder="请输入广告平台" class="width100" filterable clearable>
          <el-option
            v-for="item in selOptions.adplatform"
            :key="item.value"
            :value="item.value"
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="vip类型" prop="viptype">
<!--        <el-input v-model="dataForm.viptype" placeholder="请输入vip类型" size="small"></el-input>-->
        <el-select v-model="dataForm.viptype" placeholder="请输入vip类型" class="width100" filterable clearable>
          <el-option
            v-for="item in selOptions.viptype"
            :key="item.value"
            :value="item.value"
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序">
        <el-input type="number" v-model="dataForm.ordersum" placeholder="请输入排序" size="small"></el-input>
      </el-form-item>
    </el-form>
    <div
      :style="{position: 'absolute',right: 0,bottom: 0,width: '100%', borderTop: '1px solid #e9e9e9',padding: '10px 16px',background: '#fff',textAlign: 'right',zIndex: 1,}">
      <el-button @click="cancel()" :style="{ marginRight: '8px' }">取消</el-button>
      <el-button type="primary" @click="submit()">确定</el-button>
    </div>
  </a-drawer>
</template>

<script>
import api from '@/api/api'
import uploadImg from '@/components/Upload/uploadImg.vue'
import utils from '@/api/utils'

export default {
  components: { uploadImg },
  data() {
    return {
      visible: false,
      selOptions: {},
      applist:[],
      loading: false,
      dataForm: {
        id: 0,
        appid:'',
        taskname: '',
        taskinfo: '',
        coin: '',
        limitcount: '',
        adtype: '',
        adplatform: '',
        viptype: '',
        ordersum: '',
        status: '',
        icon: '',
      },
      dataRule: {
        appid: [
          { required: true, message: 'app  为必填项', trigger: 'blur' }
        ],
        taskname: [
          { required: true, message: '任务名称  为必填项', trigger: 'blur' }
        ],
        taskinfo: [
          { required: true, message: '任务描述  为必填项', trigger: 'blur' }
        ],
        coin: [
          { required: true, message: '金币  为必填项', trigger: 'blur' }
        ],
        limitcount: [
          { required: true, message: '任务数量  为必填项', trigger: 'blur' }
        ],
        adtype: [
          { required: true, message: '广告类型   为必填项', trigger: 'blur' }
        ],
        adplatform: [
          { required: true, message: '广告平台   为必填项', trigger: 'blur' }
        ],
        viptype: [
          { required: true, message: 'vip类型  为必填项', trigger: 'blur' }
        ],
        ordersum: [
          { required: true, message: '排序  为必填项', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态  为必填项', trigger: 'blur' }
        ],
        createtime: [
          { required: true, message: '创建时间  为必填项', trigger: 'blur' }
        ],
        updatetime: [
          { required: true, message: '更新时间  为必填项', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) { // 初始化表单验证规则
      this.dataForm.id = id || undefined
      this.visible = true
      this.selOptions = this.$constants.selOptions
      utils.getAppList().then(res => {this.applist=res.datalist});
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          api.post('rest/vabc/task/info', { id: this.dataForm.id }).then(res => {
            this.dataForm = res
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    // 表单数据提交
    submit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          let postData = Object.assign({}, this.dataForm)
          if (!this.dataForm.id) {
            postData.id == undefined
            api.post('rest/vabc/task/save', postData).then(res => {
              this.$message.success('创建成功')
              this.reset()
              this.visible = false
              this.$parent.getDataList()
            }).finally(() => {
              this.loading = false
            })
          } else {
            api.post('rest/vabc/task/update', postData).then(res => {
              this.$message.success('保存成功')
              this.reset()
              this.visible = false
              this.$parent.getDataList()
            }).finally(() => {
              this.loading = false
            })
          }
        }
      })
    },
    cancel() {
      this.visible = false
      this.reset()
    },
    reset() {
      this.$refs.dataForm.resetFields()
      this.$data.dataForm = JSON.parse(JSON.stringify(this.$options.data().dataForm))
      //图片回写
      //this.$refs["childData"].fileList=[]
    }
  }
}
</script>
